﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using BluePortal.Framework;

namespace BluePortal.Medias.Core
{
    public class BCTag : AbstractBusinessComponent
    {
        #region IVideoTagService Members

        public IEnumerable FindAllForTagCloud()
        {
            BCVideo vs = new BCVideo();
            List<Video> videos = vs.QueryFindAll().ToList();
            var query = from v in vs.QueryFindAll()
                        from tag in v.MediaTags
                        group tag by tag.Tag.TagName into g
                        orderby g.Key
                        select new { Name = g.Key, Weight = g.Count() };
            var l = query.ToList();
            return query;
        }

        public IEnumerable<Tag> FindAll()
        {

            var query = from t in Context.Tags
                        select t;
            
            return query;
        }

        public FindResult<Tag> FindAll(FindParameters p)
        {

            var query = from t in Context.Tags
                        select t;

            return FindResultHelper.ToFindResult<Tag>(query, p);
        }

        public IEnumerable<Tag> FindAllWithMediaTag()
        {
            var query = from vt in Context.MediaTags
                        join t in Context.Tags on vt.TID equals t.TID
                        select t;
            return query;
        }

        public IEnumerable<Tag> FindStartWith(string s)
        {
            var query = from t in Context.Tags
                        where t.TagName.StartsWith(s) 
                        select t;

            return query;
        }

        private void NewTag(Tag t)
        {
            t.CreationDate = DateTime.Now;
            Context.Tags.InsertOnSubmit(t);
            Context.SubmitChanges();
        }

        public void Add(Media media, Tag t)
        {
            Context.MediaTags.InsertOnSubmit(new MediaTag() { TID = t.TID, ID = media.ID });
            Context.SubmitChanges();
        }

        public void Add(Media media, String st)
        {
            Tag t = Context.Tags.SingleOrDefault(tag => tag.TagName == st);

            if (t == null)
            {
                Guid g = Guid.NewGuid();
                NewTag(new Tag() { TID = g, TagName = st });
                Context.MediaTags.InsertOnSubmit(new MediaTag() { TID = g, ID = media.ID });
                Context.SubmitChanges();
            }
            else
            {
                if (Context.MediaTags.SingleOrDefault(vt => vt.ID == media.ID && vt.TID == t.TID) == null)
                {
                    Context.MediaTags.InsertOnSubmit(new MediaTag() { TID = t.TID, ID = media.ID });
                    Context.SubmitChanges();
                }
            }
        }

        public IEnumerable<Tag> FindByID(Guid ID)
        {
            var query = from t in Context.Tags
                        from vt in t.MediaTags
                        where vt.ID == ID
                        select t;
            return query;
        }

        public void Remove(Guid ID, Guid tid)
        {
            var mtag = Context.MediaTags.Single(vt => vt.ID == ID && vt.TID == tid);
            Context.MediaTags.DeleteOnSubmit(mtag);
            Context.SubmitChanges();
        }

        public IEnumerable<Tag> FindByMedia(Media media)
        {
            var query = from t in Context.Tags
                        from vt in t.MediaTags
                        where vt.ID == media.ID
                        select t;
            return query.AsEnumerable();
        }

        #endregion


        public IEnumerable ExtractTagCloud(IQueryable<Media> qVideo)
        {
            var query = from v in qVideo
                        from tag in v.MediaTags
                        group tag by tag.Tag.TagName into g
                        orderby g.Key
                        select new { Name = g.Key, Weight = g.Count() };
            return query;
        }

        public IEnumerable ExtractTagCloud(Guid vid)
        {
            return Enumerable.Empty<MediaTag>();
        }
    }
}
